package net.monare.mifss.evn;

import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Notification;
import net.monare.mifss.MifssMessages;
import net.monare.mifss.admin.ui.mod.StorageDefUiModule;
import net.monare.mifss.admin.ui.win.ConfigureStorageWindow;
import net.monare.mifss.configurations.pojo.Property;
import net.monare.mifss.configurations.pojo.StorageDef;
import net.monare.mifss.exceptions.StorageServiceException;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


public class UpdateStorageDefinitionListener extends BasicMifssListener implements ClickListener {

    private static final long serialVersionUID = -6512732214521808807L;

    private StorageDefUiModule storageDefUiModule;
    private ConfigureStorageWindow parent;


    public UpdateStorageDefinitionListener(StorageDefUiModule storageDefUiModule, ConfigureStorageWindow parent) {

        this.storageDefUiModule = storageDefUiModule;
        this.parent = parent;
    }

    public void buttonClick(ClickEvent event) {

        try {

            StorageDef storageDef = storageDefUiModule.getStorageDef();

            if (storageDef != null) {

                // load storage def property names
                List<String> oldPropertyNames = new LinkedList<String>();
                for(Property oldProperty : getMifssHolder().getStorageService().getStorageDefinition(storageDef.getName()).getProperties()){
                    oldPropertyNames.add(oldProperty.getName());
                }

                getMifssHolder().getStorageService().modifyStorageDescription(storageDef.getName(), storageDef.getDescription());
                getMifssHolder().getStorageService().modifyStorageMode(storageDef.getName(), storageDef.getMode());
                getMifssHolder().getStorageService().modifyStorageTitle(storageDef.getName(), storageDef.getTitle());
                getMifssHolder().getStorageService().modifyStorageTransformation(storageDef.getName(), storageDef.getTransformationHandlerName());

                // set storage definition properties
                for (Property property : storageDef.getProperties()) {
                    getMifssHolder().getStorageService().modifyStorageProperty(storageDef.getName(), property);
                }

                // remove unlisted properties
                for(String oldPropertyName : oldPropertyNames){
                    boolean propertyListed = false;

                    // is old property listed in new property list?
                    for (Property newProperty : storageDef.getProperties()) {

                        if (newProperty.getName().equals(oldPropertyName)) {
                            propertyListed = true;
                            break;
                        }

                    }

                    // remove old property
                    if (!propertyListed) {
                        getMifssHolder().getStorageService().removeStorageProperty(storageDef.getName(), oldPropertyName);
                    }
                }



                parent.resetAllPanes();
                Notification.show(MifssMessages.getMessage("sys.success.storage.updated"));
            } else {
                Notification.show(MifssMessages.getMessage("sys.error.storage.def.not.defined"));
            }

        } catch (StorageServiceException e) {
            Notification.show(MifssMessages.getMessage("sys.error.storage.def.update") + " : " + e.getMessage());
        }

    }

}
